<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Entities\Module;
use Modules\Admin\Service\ModuleService;

class ModuleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$data=Module::get();
        return view('admin::module.index',compact('data'));
    }
    //更新模块功能
	public function updateCache(ModuleService $moduleService){
		$modules=$moduleService->updateModule ();
		foreach($modules as $module){
			Module::create([
				'name'=>$module['name'],
				'title'=>$module['title'],
				'isdefault'=>$module['isdefault'],
				'home_access'=>$module['home_access']
			]);
		}
		session()->flash('success','更新成功！');
		return redirect ()->back();
	}

	//设置默认模板
	public function setDefaultModule($name){
    	\DB::table('modules')->update(['isdefault'=>0]);
		$module=Module::where('name','=',$name)->first();
		$module->isdefault=1;
		$module->save();
		session()->flash('success','设置成功！');
		return redirect ()->back();
	}
}
